package edu.zbtc.sunxd.retrofittest6;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CarFragment extends Fragment {
    private RecyclerView mRV;
    private List<Car> mData;
    private CarAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_car, container, false);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.166:8085")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CarService carService = retrofit.create(CarService.class);
        carService.getAll().enqueue(new Callback<CarResponse>() {
            @Override
            public void onResponse(Call<CarResponse> call, Response<CarResponse> response) {
                CarResponse body = response.body();
                if (body.getStatus() == 200) {
                    mData = body.getData();
                    Toast.makeText(getActivity(), "从服务器获得了" + mData.size() + "个数据", Toast.LENGTH_SHORT).show();
                    mAdapter = new CarAdapter();
                    mRV.setAdapter(mAdapter);
                } else {
                    Toast.makeText(getActivity(), "服务器请求失败，原因：" + body.getMessage(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<CarResponse> call, Throwable t) {
                Log.e("RetrofitTest", "=====网络通信失败=====", t);
                Toast.makeText(getActivity(), "网络通信失败", Toast.LENGTH_SHORT).show();
            }
        });

        mRV = view.findViewById(R.id.rv);
        mRV.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        return view;
    }

    class CarViewHolder extends RecyclerView.ViewHolder {
        private TextView mCarId;
        private TextView mCarName;
        private TextView mContent;

        public CarViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_car, parent, false));
            mCarId = itemView.findViewById(R.id.car_id);
            mCarName = itemView.findViewById(R.id.car_name);
            mContent = itemView.findViewById(R.id.content);
        }

        void bind(Car car) {
            mCarId.setText(car.getId().toString());
            mCarName.setText(car.getCarName());
            mContent.setText(car.getContent());
        }
    }

    class CarAdapter extends RecyclerView.Adapter<CarViewHolder> {

        @NonNull
        @Override
        public CarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new CarViewHolder(LayoutInflater.from(getActivity()), parent);
        }

        @Override
        public void onBindViewHolder(@NonNull CarViewHolder holder, int position) {
            holder.bind(mData.get(position));
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }
}
